POJ - 2240 Arbitrage (最短路 Floyd算法 && Bellman-Ford算法)

Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent. 

Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not. 
Input
The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible. 
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".
Sample Input
3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

0
Sample Output
Case 1: Yes

Case 2: No

题意:

钱币套汇问题,就是利用货币之间的汇率差异,从而将一单位的某种货币,兑换回多于一单位的同种货币。题目每组数据给出N种货币,M种汇率关系,每种关系是每单位的前者兑换后者的汇率(单向)。问通过这些汇率兑换会不会出现套汇现象。

解题思路:

其实每种货币可以看成一个点,货币之间的汇率看成这两点构成的边的权值,构造好图之后就是求从一点出发,看经过几条路径之后可以得到比自身大的值(但是注意路径上的权值是相乘的关系不是相加)。

Floyd算法:

 因为最多30种货币,所以用Flayd算法也可以,Floyd算法的思想也比较好理解,主要就是图的构造,(看代码注释)需要注意的是,题目要求最大汇率关系,所以更新数据时是求大于本身的,并且权值为相乘关系,具体看代码注释。

AC代码:

#include<stdio.h>
#include<string.h>

char huo[36][36];
char aa[36], bb[36];
double mp[36][36], x;

int main()
{
    int n, g = 1;
    while( scanf("%d",&n) && n )
    {
        for( int i = 1; i <= n; i++ )
            scanf("%s",huo[i]);     //存入货币种类
        int m;                      //初始化图,因为没有汇率关系的
        memset(mp,0,sizeof(mp));    //货币之间不能兑换,赋值为0
        for( int i = 1; i <= n; i++ )
            mp[i][i] = 1;           //同种货币之间汇率为1
        scanf("%d",&m);
        for( int i = 1; i <= m; i++ )
        {
            int x1, x2;
            scanf("%s%lf%s",aa,&x,bb);
            for( int j = 1; j <= n; j++ )
            {
                if( strcmp(huo[j],aa) == 0 ) //查找是第几种货币,转化为点
                {
                    x1 = j;
                    break;
                }
            }
            for( int j = 1; j <= n; j++ )
            {
                if( strcmp(huo[j],bb) == 0 ) //查找是第几种货币,转化为点
                {
                    x2 = j;
                    break;
                }
            }
            mp[x1][x2] = x;   //赋权值
        }
        for( int k = 1; k <= n; k++ )
        {
            for( int i = 1; i <= n; i++ )
            {
                for( int j = 1; j <= n; j++ )
                {
                    if( mp[i][j] < mp[i][k] * mp[k][j] ) //因为找最大汇率关系,所以为“<”和“*”
                        mp[i][j] = mp[i][k] * mp[k][j];
                }
            }
        }
        int flag = 0;
        for( int i = 1; i <= n; i++ )
        {
            if( mp[i][i] > 1 ) //发现有超过本身的值,说明有套汇现象
            {
                flag = 1;
                break;
            }
        }
        printf("Case %d: ",g++);
        if( flag )
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

Bellman-Ford算法:

注意之前该算法从源点V0到顶点V的最短路经最多有n-1条边;但在本题中,由于要找到一条回路,回到源点V0,所以最多有n条边,且多于n条边是没有必要的,因为如果存在套汇的话,n条边构成的回路就能形成套汇,具体看代码注释。

AC代码:

#include<stdio.h>
#include<string.h>
#define maxn 50 //顶点数最大值
#define maxm 1000 //边数最大值
#define max(a,b) ( (a)>(b) ? (a):(b) )

struct exchange //汇率关系
{
    int ci, cj;
    double cij;
}ex[maxm]; //汇率关系数组

int i, j, k; //循环变量
int n, m;    //货币种类、汇率数目
int flag, kase = 0;
char name[maxn][20], a[20], b[20];
double x, maxdist[maxn]; //汇率、源点i到其他每个顶点(包括它本身)的最长路径长度

int readcase() //读入数据
{
    scanf("%d",&n);
    if( n == 0 )
        return 0;
    for( i = 0; i < n; i++ ) //读入n个货币名称
        scanf("%s",name[i]);
    scanf("%d",&m);
    for( int i = 0; i < m; i++ ) //读入汇率
    {
        scanf("%s%lf%s",a,&x,b);
        for( j = 0; strcmp(a,name[j]); j++ )
            ;
        for( k = 0; strcmp(b,name[k]); k++ )
            ;
        ex[i].ci = j; ex[i].cij = x; ex[i].cj = k;
    }
    return 1;
}

//Bellman-Ford算法;以顶点v0为源点,求它到每个顶点(包括它本身)的最大距离
void bellman( int v0 )
{
    flag = 0;
    memset(maxdist,0,sizeof(maxdist));
    maxdist[v0] = 1;
    for( k = 0; k < n; k++ ) //从maxdist(0)递推到maxdist(1),...,maxdist(n)
    {
        for( i = 0; i < m; i++ ) //判断每条边,加入它是否能使得最大距离增加
        {
            if( maxdist[ex[i].ci] * ex[i].cij > maxdist[ex[i].cj] )
                maxdist[ex[i].cj] = maxdist[ex[i].ci] * ex[i].cij;
        }
    }
    if( maxdist[v0] > 1.0 )
        flag = 1;
}

int main()
{
    while( readcase() ) //读入货币种类
    {
        for( i = 0; i < n; i++ )
        {
            bellman(i);  //从第i个顶点出发求最长路径
            if( flag ) break;
        }
        if(flag) printf("Case %d: Yes\n",++kase);
        else printf("Case %d: No\n",++kase);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值